from osv import fields, osv
from tools.translate import _

class product_product(osv.osv):
    
    _description = "Add Budget Link to Product"
    _inherit = "product.product"
    _name = "product.product"
    _inherit = "product.product"
    
    _columns={
              'kdvn_budget_id':fields.many2one("account.budget.post","Budget")
              }
product_product()


class account_budget_post(osv.osv):
    _name = 'account.budget.post'
    _description = 'Budgetary Position'
    _inherit='account.budget.post'
    _columns={
              'set_default':fields.boolean('Set default value',help='If set default this budget automatically fill-in 0 when project created')
              }
    _defaults={
               'set_default':lambda *a:False
               }
    
    def name_get(self, cr, uid, ids, context={}):
        if not len(ids):
            return []
        reads = self.read(cr, uid, ids, ['code','name'], context)
        res = []
        for record in reads:
            name = record['name']
            name = record['code']+' - '+name
            res.append((record['id'], name))
        return res
    
    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        ids = []
        if name:
            ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('code',operator,name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit,context=context)
        return self.name_get(cr, user, ids, context=context)

account_budget_post()
